const { register, checkUsername, login, getLessors, getState,getLessorsOrders } = require('../dao/lessorsDao')

module.exports.register = async function (msg) {
    const data = await register(msg)
    return {
        data,
        status: 1,
        message: '注册成功'
    }
}
module.exports.checkUsername = async function (username) {
    const data = await checkUsername(username)
    if (data.length == 0) {
        return {
            status: 1,
            message: "可以注册"
        }
    }
    return {
        status: 0,
        message: '已经被注册'
    }
}

module.exports.login = async function (username) {
    const data = await login(username)
    return data
}

module.exports.getLessorsOrders = async function (id) {
    
    const { lessorsId, type, value,pageSize,currentPage } = id;
    const data = await getLessorsOrders(id)
    
    //搜索后的数据
    const data1= data.filter(item => {

        switch (type) {
            case "startTime":
                if (item.startTime.includes(value)) {
                    return item
                }
                break;

            case "endTime":
                if (item.startTime.includes(value)) {
                    return item
                }
                break;
            case "carNumber": 
                if (item.parkingsId.carNumber.includes(value)){
                    return item
                }
                           
               break;    
            case "name": 
           
               if (item.tenantsId.name.includes(value)){
                   return item
               }   

        }
    })
    //分页
    const allPages=data1.length
    let data2=[]
    if(pageSize*currentPage>data1.length){
        data2=data1.slice((currentPage-1)*pageSize )
    }else{
        data2=data1.slice((currentPage-1)*pageSize, currentPage*pageSize )
    }


    return {data2,allPages}
}


module.exports.getLessors = async function (msg) {
    const data = await getLessors(msg);
    if (data.tableData.length > 0) {
        return {
            status: '1',
            data
        }
    } else {
        return {
            status: '0'
        }
    }
}


module.exports.getState = async function (msg) {
    const data = await getState(msg);
    if (data) {
        return {
            status: '1',
        }
    } else {
        return {
            status: '0'
        }
    }
}
